﻿using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;

namespace SmlEngine
{
    public static class Vector2Extensions
    {
        /// <summary>
        /// Rounds a vector to integers.
        /// </summary>
        /// <returns>The integerized vector.</returns>
        public static Vector2 ToIntVector(this Vector2 vector)
        {
            return new Vector2((int)vector.X, (int)vector.Y);
        }

        /// <summary>
        /// Creates a rectangle using a given size.
        /// </summary>
        /// <param name="width">The width of the rectangle.</param>
        /// <param name="height">The height of the rectangle.</param>
        /// <returns></returns>
        public static Rectangle ToRectangle(this Vector2 vector, int width, int height)
        {
            return new Rectangle((int)vector.X, (int)vector.Y, width, height);
        }

        /// <summary>
        /// Creates a rectangle using a given size vector.
        /// </summary>
        /// <param name="size">The size of the rectangle.</param>
        /// <returns></returns>
        public static Rectangle ToRectangle(this Vector2 vector, Vector2 size)
        {
            return ToRectangle(vector, (int)size.X, (int)size.Y);
        }

        /// <summary>
        /// Converts a list of vectors into a list of Rectangle using a size vector.
        /// </summary>
        /// <param name="size">The size vector.</param>
        /// <returns>A list of Rectangles.</returns>
        public static List<Rectangle> ToRectangleList(this List<Vector2> vectors, Vector2 size)
        {
            List<Rectangle> result = new List<Rectangle>();
            foreach (Vector2 v in vectors)
            {
                result.Add(v.ToRectangle(size));
            }
            return result;
        }

        /// <summary>
        /// Floors the components of a vector.
        /// </summary>
        /// <returns>The floored vector.</returns>
        public static Vector2 Floor(this Vector2 v)
        {
            return new Vector2((float)Math.Floor(v.X), (float)Math.Floor(v.Y));
        }

        /// <summary>
        /// Floors and divides a vector.
        /// </summary>
        /// <param name="divideBy">The divisor.</param>
        /// <returns>The floored and divided vector.</returns>
        public static Vector2 FloorDivide(this Vector2 v, float divideBy)
        {
            return (v / divideBy).Floor();
        }

        /// <summary>
        /// Compares a vector and a two-dimensional point.
        /// </summary>
        /// <param name="p">The point to compare with.</param>
        /// <returns>If the point is equal to the vector.</returns>
        public static bool ComparePoint(this Vector2 v, Point p)
        {
            return (v.X == p.X && v.Y == p.Y);
        }
    }
}
